// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

var DataMgr = require('DataMgr');
var LayerMgr = require('LayerMgr');

cc.Class({
    extends: cc.Component,

    properties: {
        nodePlate: cc.Node, // 板子
        nodeTouch: cc.Node, // 触摸节点
        nodeLipstick: cc.Node, // 飞刀位置
        nodeLevelLabel:cc.Label, // 关卡文本
        prefabLipstick: cc.Prefab, // 飞刀预制
        nodeLipstickContainer: cc.Node, // 总共有多少飞刀容器
        nodeFloatingShield: cc.Node, // 悬浮物
        PlateAnimation:cc.Node, // 骨骼动画
    },

    // LIFE-CYCLE CALLBACKS:
    // gotoAndPlay

    onLoad() {
        let mgr = cc.director.getCollisionManager();
        mgr.enabled = true;
        this.nodeFloatingShield.x = -300;
        // mgr.enabledDebugDraw = true;
        // mgr.enabledDrawBoundingBox  = true;
    },

    start() {
        this.nodeTouch.getComponent('Touch').touchCallback(() => {
            this.onTouchCallback();
        });

        this.reloadUI();
        this.startRun();
    },

    startRun() {
        // 顺时针转一圈，再逆时针转一圈，如此反复
        this.nodePlate.stopAllActions();
        this.nodePlate.rotation = 0;

        let seq = cc.sequence(
            cc.rotateBy(4, 360).easing(cc.easeIn(1.5)),
            cc.rotateBy(4, -360).easing(cc.easeIn(1.5)),
        );
        this.nodePlate.runAction(seq.repeatForever());

        // this.nodeFloatingShield.stopAllActions();
        // var speed = DataMgr.levelInfo[DataMgr.currentLevel - 1].crowSpeed;
        // var callback = cc.callFunc(this.turnBack, this);
        // let seqfloat = cc.sequence(
        //     callback,
        //     cc.moveTo(2, cc.v2(250, -25)),
        //     callback,
        //     cc.moveTo(2, cc.v2(-300, -25)),
        // ).speed(speed);
        
        // this.nodeFloatingShield.runAction(seqfloat.repeatForever());
    },

    turnBack: function () {
        // 乌鸦调头
        this.nodeFloatingShield.scaleX = -this.nodeFloatingShield.scaleX;
    },

    /**
     * @description: 点击屏幕回调
     * @param {type} 
     * @return: 
     */
    onTouchCallback() {
        // 飞刀用完了
        if (DataMgr.leftLipstick <= 0) {
            return;
        }

        if (this.nodeLipstick.active) {
            let node = cc.instantiate(this.prefabLipstick);
            node.x = this.nodeLipstick.x;
            node.y = this.nodeLipstick.y;
            node.parent = this.nodeLipstick.parent;
            node.runAction(cc.moveTo(0.5, 0, this.nodePlate.y));
            node.getComponent('Lipstick').setCallback({
                success: () => {
                    this.onSuccessCallback();
                },
                fail: () => {
                    this.onFailCallback();
                }
            });
            DataMgr.leftLipstick--;

            let js = this.nodeLipstickContainer.getComponent('LipstickContainer');
            js.reload(DataMgr.leftLipstick, DataMgr.levelInfo[DataMgr.currentLevel - 1].totalLipstick);

            if (DataMgr.leftLipstick <= 0) {
                this.nodeLipstick.active = false;
            }
            else {
                this.nodeLipstick.runAction(cc.sequence(
                    cc.hide(),
                    cc.delayTime(0.2),
                    cc.show(),
                ));
            }

        }
    },

    /**
     * @description: 重新加载UI
     * @param {type}
     * @return:
     */
    reloadUI() {

        this.nodePlate.active = true;
        this.PlateAnimation.active = false;

        this.nodeLevelLabel.string = "LEVEL" + DataMgr.currentLevel;
        this.nodePlate.stopAllActions();
        this.nodePlate.rotation = 0;

        let seq = cc.sequence(
            cc.rotateBy(4, 360).easing(cc.easeIn(1.5)),
            cc.rotateBy(4, -360).easing(cc.easeIn(1.5)),
        );
        this.nodePlate.runAction(seq.repeatForever());

        this.nodeFloatingShield.x = -300;
        this.nodeFloatingShield.scaleX = 0.15;
        this.nodeFloatingShield.stopAllActions();
        var speed = DataMgr.levelInfo[DataMgr.currentLevel - 1].crowSpeed;
        var callback = cc.callFunc(this.turnBack, this);
        let seqfloat = cc.sequence(
            callback,
            cc.moveTo(2, cc.v2(250, -25)),
            callback,
            cc.moveTo(2, cc.v2(-300, -25)),
        ).speed(speed);
        
        this.nodeFloatingShield.runAction(seqfloat.repeatForever());

        // 关卡信息
        let levelInfo = DataMgr.levelInfo[DataMgr.currentLevel - 1];

        // 游戏开始提示
        LayerMgr.showLevelStart(levelInfo.level);

        // 飞刀数量
        let js = this.nodeLipstickContainer.getComponent('LipstickContainer');
        js.reload(levelInfo.totalLipstick, levelInfo.totalLipstick);

        // 数据清理
        DataMgr.leftLipstick = levelInfo.totalLipstick;
        DataMgr.hitLipstick = 0;

        // 转盘
        this.nodePlate.getComponent('Plate').removeAllLipstick();
        // this.nodeFloatingShield.getComponent('FloatingShield').removeAllLipstick();

        this.nodeLipstick.active = (levelInfo.totalLipstick > 0);
    },


    /**
     * @description: 单个飞刀命中目标
     * @param {type} 
     * @return: 
     */
    onSuccessCallback() {
        DataMgr.hitLipstick++;

        if (DataMgr.hitLipstick == DataMgr.levelInfo[DataMgr.currentLevel - 1].totalLipstick) {
            // 通关
            if (DataMgr.currentLevel == DataMgr.levelInfo[DataMgr.levelInfo.length - 1].level) {
                cc.sys.localStorage.setItem('level', 1);
                DataMgr.reset();
                cc.director.loadScene("win");
            }
            // 闯关
            else {
                DataMgr.currentLevel++;
                this.nodePlate.active = false;
                this.PlateAnimation.active = true;
                this.node.runAction(cc.sequence(
                    // cc.callFunc(() => {
                    //     LayerMgr.showLevelEnd(true);
                    // }),
                    cc.delayTime(1.14),
                    cc.callFunc(() => {
                        this.reloadUI();
                    }),
                ));
                
            }
        }

        // 未全部命中
        else {

        }
    },

    /**
     * @description: 单个飞刀未命中目标
     * @param {type} 
     * @return: 
     */
    onFailCallback() {
        
        if(DataMgr.currentLevel <= 2) {
            
            setTimeout(() => {
                LayerMgr.showLevelEnd(false);
                cc.sys.localStorage.setItem('level', DataMgr.currentLevel);
                DataMgr.reset();
            }, 200);
            
        }else {
            setTimeout(() => {
                cc.director.loadScene("resurrection");
            }, 200);
            
        }
        
    },

});
